package net.wds.study.teacher.controller;/**
 * Package: net.wds.study.teacher.controller
 * Description： TODO
 * Author: 作者
 * Date: Created in 2020/9/9 9:14
 * Company: 公司
 * Version: 0.0.1
 */

import net.sf.json.JSONObject;
import net.wds.study.student.modal.PageModal;
import net.wds.study.teacher.entity.Teacher;
import net.wds.study.teacher.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;

/**
 * @ClassName : TeacherController
 * @Description :
 * @Author : HTB
 * @Date: 2020-09-09 09:14
 */
@Controller
@RequestMapping("/tea")
public class TeacherController {
    @Resource
    private TeacherService teacherService;

    @RequestMapping(value = "/getPage",method = {RequestMethod.POST})
    private void getPage(@RequestParam Integer startIndex,
                         @RequestParam Integer pageSize,
                         @RequestParam String name,
                         @RequestParam Integer sex,
                         HttpServletResponse response){
        PageModal<Teacher> paramByParams = teacherService.getParamByParams(startIndex, pageSize, name, sex);
        try{
            response.getWriter().write(JSONObject.fromObject(paramByParams).toString());
        }catch(IOException e){
            e.printStackTrace();
        }
    }
    @RequestMapping(value="/addTeacher",method ={RequestMethod.POST})
    public void addTeacher(Teacher teacher,HttpServletResponse response){
        try {
            Serializable serializable = teacherService.addTeacher(teacher);
            response.getWriter().write("{\"success\":true,\"id\":"+serializable+",\"message\":\"保存成功\"}");
        } catch (Exception e) {
            e.printStackTrace();
            try {
                response.getWriter().write("{\"success\":false,\"id\":null,\"message\":\"保存失败\"}");
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }

    @RequestMapping(value="/updateTeacher",method ={RequestMethod.POST})
    public void updateTeacher(Teacher teacher,HttpServletResponse response){
        try {
            teacherService.updateTeacher(teacher);
            response.getWriter().write("{\"success\":true,\"id\":null,\"message\":\"修改成功\"}");
        } catch (Exception e) {
            e.printStackTrace();
            try {
                response.getWriter().write("{\"success\":false,\"id\":null,\"message\":\"修改失败\"}");
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }

    @RequestMapping(value="/deleteTeacher",method = {RequestMethod.POST,RequestMethod.GET})
    public void delete(@RequestParam Integer id,HttpServletResponse response){
        try {
            teacherService.deleteTeacherById(id);
            response.getWriter().write("{\"success\":true,\"id\":null,\"message\":\"删除成功\"}");
        } catch (Exception e) {
            e.printStackTrace();
            try {
                response.getWriter().write("{\"success\":false,\"id\":null,\"message\":\"删除失败\"}");
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }

    }

    @RequestMapping(value="/toAdd",method = {RequestMethod.POST})
    public String toAdd(){
        return "forward:/teacher/js/teacher.html";
    }
}
